package me.jiatao.baselibrary.rx


import com.trello.rxlifecycle3.LifecycleProvider
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import me.jiatao.baselibrary.data.protocol.HttpResult


/**
 * 扩展Observable执行
 */
fun <T> Observable<T>.execute(
    subscriber: BaseSubscriber<T>,
    lifecycleProvider: LifecycleProvider<*>
) {
    this.compose(lifecycleProvider.bindToLifecycle())
        .retryWhen(RetryWithDelay())
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(subscriber)
}

/**
 *  扩展数据转换
 */
fun <T> Observable<HttpResult<T>>.convert(): Observable<T> {
    return this.flatMap(BaseFunc())
}

/**
 *  扩展Boolean类型数据转换
 */
fun <T> Observable<HttpResult<T>>.convertBoolean(): Observable<Boolean> {
    return this.flatMap(BaseFuncBoolean())
}